<template>
  <div class="edit-popup">
    <popup
      ref="popupRef"
      :title="popupTitle"
      :async="true"
      width="550px"
      @confirm="handleSubmit"
      @close="handleClose"
    >
      <el-form
        ref="formRef"
        :model="formData"
        label-width="100px"
        :rules="formRules"
      >
        <el-form-item label="优惠券名称" prop="couponName">
          <el-input
            placeholder="请输入优惠券名称"
            v-model="formData.couponName"
            clearable
            :maxlength="100"
          />
        </el-form-item>
        <el-form-item label="优惠券金额" prop="couponAmount">
          <el-input-number
            v-model="formData.couponAmount"
            :max="999"
            :min="0"
            :precision="2"
            controls-position="right"
            size="large"
          />
        </el-form-item>
        <el-form-item label="用户ID" prop="userId">
          <el-input
            v-model="formData.userId"
            placeholder="请输入用户ID"
            clearable
            :maxlength="100"
          />
        </el-form-item>
        <el-form-item label="有效期" prop="valid">
          <el-input-number
            v-model="formData.valid"
            :max="999"
            :min="0"
            :precision="2"
            controls-position="right"
            size="large"
          />
          <text class="item-tips">天</text>
        </el-form-item>
      </el-form>
    </popup>
  </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from "element-plus";
import Popup from "@/components/popup/index.vue";
import { useDictOptions } from "@/hooks/useDictOptions";
import Upload from "@/components/upload/index.vue";
import feedback from "@/utils/feedback";
import { createCoupon } from "@/api/admin/index";

const emit = defineEmits(["success", "close"]);
const formRef = shallowRef<FormInstance>();
const popupRef = shallowRef<InstanceType<typeof Popup>>();
const mode = ref("add");
const popupTitle = computed(() => {
  return mode.value == "edit" ? "编辑" : "新建";
});
const formData = reactive({
  userId: "",
  couponName: "",
  couponAmount: 0 as number,
  valid: 0 as number,
});
const formRules = {
  userId: [
    {
      required: true,
      message: "请输入优惠券名称",
      trigger: ["blur"],
    },
  ],
  couponName: [
    {
      required: true,
      message: "请输入优惠券金额",
      trigger: ["blur"],
    },
  ],
  couponAmount: [
    {
      required: true,
      message: "请输入用户ID",
      trigger: ["blur"],
    },
  ],
  valid: [
    {
      required: true,
      message: "选择有效日期",
      trigger: ["blur"],
    },
  ],
};

const handleSubmit = async () => {
  await formRef.value?.validate();
  await createCoupon(formData);
  popupRef.value?.close();
  feedback.msgSuccess("操作成功");
  emit("success");
};

const open = (type = "add") => {
  mode.value = type;
  popupRef.value?.open();
};

const setFormData = (data: Record<any, any>) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      //@ts-ignore
      formData[key] = data[key];
    }
  }
};

const handleClose = () => {
  emit("close");
};

defineExpose({
  open,
  setFormData,
});
</script>
